package web.controller;

import config.SpringContextUtil;
import entity.Instructor;
import entity.Student;
import entity.User;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import service.InstructorService;
import service.StudentService;
import service.UserService;
import serviceImpl.InstructorServiceImpl;
import serviceImpl.StudentServiceImpl;
import serviceImpl.UserServiceImpl;

import java.io.IOException;
import java.util.Objects;

@WebServlet(name = "login", urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    /*
     * 各个页面
     * */
    private final String login_page = "/WEB-INF/views/login.jsp";
    private final String user_index_page = "/WEB-INF/views/user/user_index.jsp";
    private final String student_index_page = "/WEB-INF/views/student/student_index.jsp";
    private final String instructor_index_page = "/WEB-INF/views/instructor/instructor_index.jsp";
    @Autowired
    private UserService userService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private InstructorService instructorService;

    @Override
    public void init() throws ServletException {
        try {
            this.userService = (UserServiceImpl) SpringContextUtil.getBean("userServiceImpl", UserServiceImpl.class);
            this.studentService = (StudentServiceImpl) SpringContextUtil.getBean("studentServiceImpl", StudentServiceImpl.class);
            this.instructorService = (InstructorServiceImpl) SpringContextUtil.getBean("instructorServiceImpl", InstructorService.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String ID = request.getParameter("ID");
        String password = request.getParameter("password");
        String identity = request.getParameter("identity");
        switch (identity) {
            case "user":
                User user = userService.getUserByUserIDAndPassword(ID, password);
                if (Objects.isNull(user)) {
                    request.setAttribute("errorMessage", "用户名或密码错误，请重新输入！");
                    request.getRequestDispatcher(login_page).forward(request, response);
                } else {
                    request.getSession().setAttribute("user", user);
                    request.setAttribute("successMessage", "登陆成功");
                    RequestDispatcher dispatcher = request.getRequestDispatcher(user_index_page);
                    dispatcher.forward(request, response);
                }
                break;
            case "student":
                Student student = studentService.getStudentByStuIDAndPassword(ID, password);
                if (Objects.isNull(student)) {
                    request.setAttribute("errorMessage", "用户名或密码错误，请重新输入！");
                    request.getRequestDispatcher(login_page).forward(request, response);
                } else {
                    request.getSession().setAttribute("student", student);
                    request.setAttribute("successMessage", "登陆成功");
                    RequestDispatcher dispatcher = request.getRequestDispatcher(student_index_page);
                    dispatcher.forward(request, response);
                }
                break;
            case "instructor":
                Instructor instructor = instructorService.getInstructorByInstIDAndPassword(ID, password);
                if (Objects.isNull(instructor)) {
                    request.setAttribute("errorMessage", "用户名或密码错误，请重新输入！");
                    request.getRequestDispatcher(login_page).forward(request, response);
                } else {
                    request.getSession().setAttribute("instructor", instructor);
                    request.setAttribute("successMessage", "登陆成功");
                    RequestDispatcher dispatcher = request.getRequestDispatcher(instructor_index_page);
                    dispatcher.forward(request, response);
                }
                break;
            default:
                request.setAttribute("errorMessage", "用户名或密码错误，请重新输入！");
                request.getRequestDispatcher(login_page).forward(request, response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

}
